package heap

import "jvmgo/classfile"

type ClassMember struct {
	accessFlags    uint16
	name           string
	descriptor     string
	class          *Class
	annotationData []byte
	signature      string
}

func (self *ClassMember) copyMemberInfo(memberInfo *classfile.MemberInfo) {
	self.accessFlags = memberInfo.AccessFlags()
	self.name = memberInfo.Name()
	self.descriptor = memberInfo.Descriptor()
}

func (self *ClassMember) isAccessibleTo(d *Class) bool {
	if self.IsPublic() {
		return true
	}
	c := self.class
	if self.IsProtected() {
		return d == c || d.isSubClassOf(c) || c.GetPackageName() == d.GetPackageName()
	}
	if !self.IsPrivate() {
		return c.GetPackageName() == d.GetPackageName()
	}
	return d == c
}

func (self *ClassMember) IsPublic() bool {
	return (self.accessFlags & ACC_PUBLIC) != 0
}

func (self *ClassMember) IsPrivate() bool {
	return (self.accessFlags & ACC_PRIVATE) != 0
}

func (self *ClassMember) IsProtected() bool {
	return (self.accessFlags & ACC_PROTECTED) != 0
}

func (self *ClassMember) Signature() string {
	return self.signature
}

func (self *ClassMember) AnnotationData() []byte {
	return self.annotationData
}
